package com.ny.zmb.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.util.StringUtil;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import com.ny.zmb.openapi.ysutil.base.MerchantHeadRequestCommand;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.common.YsPayConstant;
import com.ny.zmb.openapi.ysutil.from.QueryBankListReqCommand;
import com.ny.zmb.openapi.ysutil.from.QueryCardReqCommand;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@CrossOrigin
@RequestMapping("ys/bank")
@RestController
public class YsBankController extends EYinShengBase {

    @Resource
    private YsHttpUtil ysHttpUtil;


    /**
     * 查询支持银行卡
     * @param protocolType
     * @return
     */
    @RequestMapping("queryBankList")
    public Result queryBankList(@RequestParam(value = "protocolType",required = false,defaultValue = "01") String protocolType){
        try {
            QueryBankListReqCommand command = new QueryBankListReqCommand();
            command.setProtocolType(protocolType);
            JSONObject send = ysHttpUtil.send(ServiceEnum.QUERY_BANK_LIST, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD,ServiceEnum.QUERY_BANK_LIST.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        }catch (Exception var){
            return Result.fail(var.getMessage());
        }
    }

    /**
     * 查询卡片信息
     * @param bankAccountNo
     * @return
     */
    @RequestMapping("queryCard")
    public Result queryCard(String bankAccountNo){
        try {
            if (StringUtil.isBlank(bankAccountNo)){
                return Result.fail("卡号不能为空");
            }
            QueryCardReqCommand command = new QueryCardReqCommand();
            command.setBankAccountNo(bankAccountNo);
            JSONObject send = ysHttpUtil.send(ServiceEnum.QUERY_CARD, YsPayConstant.ENV_PRD,
                    MERCHANT_PRX, MERCHANT_PRX_PWD,ServiceEnum.QUERY_CARD.getApiName(), command);
            JSONObject result = send.getJSONObject("bizResponseJson");
            return Result.ok(result);
        }catch (Exception var){
            return Result.fail(var.getMessage());
        }
    }

}
